package org.jeecg.modules.demo.expenseAllocation.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.expenseAllocation.entity.YxExpenseAllocation;
import org.jeecg.modules.demo.expenseAllocation.service.IYxExpenseAllocationService;
import org.jeecg.modules.demo.pubuyorder.service.IPuBuyOrderService;
import org.jeecg.modules.demo.socost.entity.SoCost;
import org.jeecg.modules.demo.socost.service.ISoCostService;
import org.jeecg.modules.demo.utils.generateOddUtil;
import org.jeecg.modules.demo.warehouseorder.entity.WarehouseOrder;
import org.jeecg.modules.demo.warehouseorder.service.IWarehouseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

 /**
 * @Description: yx_expense_allocation
 * @Author: jeecg-boot
 * @Date:   2021-12-10
 * @Version: V1.0
 */
@Api(tags="yx_expense_allocation")
@RestController
@RequestMapping("/expenseAllocation/yxExpenseAllocation")
@Slf4j
public class YxExpenseAllocationController extends JeecgController<YxExpenseAllocation, IYxExpenseAllocationService> {
	@Autowired
	private IYxExpenseAllocationService yxExpenseAllocationService;
	@Autowired
	private ISoCostService soCostService;
	@Autowired
	private IWarehouseOrderService warehouseOrderService;
	@Autowired
	private IPuBuyOrderService puBuyOrderService;

	 @Autowired
	 private ISysBaseAPI sysBaseAPI;

	/**
	 * 分页列表查询
	 *
	 * @param yxExpenseAllocation
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "采购费用分摊-分页列表查询")
	@ApiOperation(value="采购费用分摊-分页列表查询", notes="采购费用分摊-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="yunxiao/expenseAllocation/YxExpenseAllocationList")
	public Result<?> queryPageList(YxExpenseAllocation yxExpenseAllocation,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		try{
			QueryWrapper<YxExpenseAllocation> queryWrapper = QueryGenerator.initQueryWrapper(yxExpenseAllocation, req.getParameterMap());
			Page<YxExpenseAllocation> page = new Page<YxExpenseAllocation>(pageNo, pageSize);
			IPage<YxExpenseAllocation> pageList = yxExpenseAllocationService.page(page, queryWrapper);
			return Result.OK(pageList);
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(300,"网络故障！");
		}
	}
	
	/**
	 *   分摊
	 *
	 * @param yxExpenseAllocation
	 * @return
	 */
	@AutoLog(value = "采购费用分摊-分摊")
	@ApiOperation(value="采购费用分摊-添加", notes="采购费用分摊-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody YxExpenseAllocation yxExpenseAllocation,HttpServletRequest req) {
		try{
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			SoCost soCost = soCostService.getById(yxExpenseAllocation.getId());
			WarehouseOrder warehouseOrder = warehouseOrderService.getByWoNo(yxExpenseAllocation.getWoNo());
			yxExpenseAllocation.setWsje(warehouseOrder.getWsje());
			yxExpenseAllocation.setMeasureunit(warehouseOrder.getMeasureunit());
			yxExpenseAllocation.setId(null);
			yxExpenseAllocation.setCinvname(warehouseOrder.getCinvname());
			yxExpenseAllocation.setAmount(warehouseOrder.getQoQuantity().doubleValue());
			yxExpenseAllocation.setSaleman(sysUser.getId());
			yxExpenseAllocation.setDepartment(soCost.getDepartment());
			yxExpenseAllocation.setTradecode(warehouseOrder.getTradecode());
			Map<String,String> map = new HashMap<>();
			String str= yxExpenseAllocationService.getMaxCode();
			String tenantId = req.getHeader("tenant-id");
			String precode = puBuyOrderService.getTeantCodeById(Integer.parseInt(tenantId)).toUpperCase();
			if(str==null){
				map.put("maxCode",null);
			}else{
				map.put("maxCode",str);
			}
			String name =precode+"FT_";
			String resultCode = generateOddUtil.createCode(map,name);
			yxExpenseAllocation.setReceiptNumber(resultCode);
			yxExpenseAllocation.setCreateTime(new Date());

			if(yxExpenseAllocationService.ftSoCost(warehouseOrder,soCost,yxExpenseAllocation)){
				return Result.OK("分摊成功！");
			}
			return Result.error(300,"网络故障！");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}
	
	/**
	 *  编辑
	 *
	 * @param yxExpenseAllocation
	 * @return
	 */
	@AutoLog(value = "采购费用分摊-编辑")
	@ApiOperation(value="采购费用分摊-编辑", notes="采购费用分摊-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody YxExpenseAllocation yxExpenseAllocation) {
		yxExpenseAllocationService.updateById(yxExpenseAllocation);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "采购费用分摊-通过id删除")
	@ApiOperation(value="采购费用分摊-通过id删除", notes="采购费用分摊-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try{
			YxExpenseAllocation expenseAllocation = yxExpenseAllocationService.getById(id);
			if(yxExpenseAllocationService.delExpenSeAllocation(expenseAllocation)){
				return Result.OK("删除成功！");
			}
			return Result.error(300,"网络故障！");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统故障！");
		}

	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "采购费用分摊-批量删除")
	@ApiOperation(value="采购费用分摊-批量删除", notes="采购费用分摊-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yxExpenseAllocationService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "采购费用分摊-通过id查询")
	@ApiOperation(value="采购费用分摊-通过id查询", notes="采购费用分摊-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		YxExpenseAllocation yxExpenseAllocation = yxExpenseAllocationService.getById(id);
		if(yxExpenseAllocation==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yxExpenseAllocation);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yxExpenseAllocation
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YxExpenseAllocation yxExpenseAllocation) {
        return super.exportXls(request, yxExpenseAllocation, YxExpenseAllocation.class, "yx_expense_allocation");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YxExpenseAllocation.class);
    }





}
